/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package post;

import java.io.IOException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.text.ParseException;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import post.dataaccess.ProductReader;
import post.dataaccess.TransactionReader;

/**
 *
 * @author Stephen
 */
public class Server implements ServerRMI {

    private static final String PRODUCT_FILE = "products.txt";
    private static final String TRANSACTION_FILE = "transaction.txt";
    private ProductReader productReader;
    private TransactionReader transactionReader;

    public Server() {
        try {
            productReader = new ProductReader(PRODUCT_FILE);
        } catch (IOException ex) {
            System.err.println("IO Exception: " + ex.toString());
        }
        try {
            transactionReader = new TransactionReader(TRANSACTION_FILE, productReader.loadCatalog());
        } catch (Exception ex) {
            System.err.println("Exception: " + ex.toString());
        }
    }

    /**
     * This method adds a new transaction to the transaction log
     * @param trans is the transaction to be added to the log
     * @throws RemoteException 
     */
    @Override
    public void addTransactions(Transaction trans) throws RemoteException {
        try {
            transactionReader.addTransaction(trans);
        } catch (IOException ex) {
            System.err.println("IO Exception: " + ex.toString());
        }
    }

    /**
     * This method is for getting the ProductCatalog
     * @return is the ProductCatalog
     * @throws RemoteException 
     */
    @Override
    public ProductCatalog getProductCatalog() throws RemoteException {
        return productReader.loadCatalog();
    }
    
    /**
     * This method is for adding another product to the ProductCatalog
     * @param upc is the UPC of the product to add
     * @param desc is the description of the product to add
     * @param price is the price of the product to add
     */
    public void addProductToCatalog(String upc, String desc, float price) {
        Product product = new Product(upc, desc, price);
        try {
            productReader.addProduct(product);
        } catch (IOException ex) {
            System.err.println("IO Exception: " + ex.toString());
        }
    }

    /**
     * This method is used for initializing the server. This method registers the
     * server in the registry using port 1099
     */
    public void init() {
        try {
            Server obj = new Server();
            ServerRMI stub = (ServerRMI) UnicastRemoteObject.exportObject(obj, 0);

            Registry registry = LocateRegistry.createRegistry(1099);
            registry.rebind("ServerRMI", stub);

            System.err.println("Server Ready");
        } catch (Exception e) {
            System.err.println("Server exception: " + e.toString());
            e.printStackTrace();
            System.exit(1);
        }
    }
    
    /**
     * This method gets all transaction done on a certain date
     * @param transDate is the date of transactions to get
     * @return is a String array containing information of all transactions on that date
     */
    public String[] getTransactionsbyDate(Date transDate) {
        try {
            updateTransactionReader();
            return transactionReader.getTransactionByDate(transDate);
        } catch (ParseException ex) {
            System.err.println("Parse Exception: " + ex.toString());
            return null;
        }
    }
    
    /**
     * This method is used to get all transactions by a certain customer
     * @param name is the name of the customer
     * @return is a String array containing the transaction info
     */
    public String[] getTransactionsByCustomer(String name) {
        updateTransactionReader();
        return transactionReader.getTransactionByCustomerName(name);
    }
    
    /**
     * This method is for updating the transactionReader to grab an updated transaction log.
     */
    private void updateTransactionReader() {
        try {
            transactionReader = new TransactionReader(TRANSACTION_FILE, productReader.loadCatalog());
        } catch (Exception ex) {
            System.err.println("Exception: " + ex.toString());
        }
    }
}
